package com.requestGwsLongGangQu;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
public class Elevator {

    private String api_key = "642ef1804ff04ffcb2f87de5ee5e55b4";
    private final String api_version = "1.0";
    private String engCode = "XM20160409";
    private String projectCode = "XM20160409";
    private String httpserver = "http://218.17.147.31:66/openapi/";

    public void setApi_key(String api_key) {
        this.api_key = api_key;
    }

    public void setEngCode(String engCode) {
        this.engCode = engCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public void setHttpserver(String httpserver) {
        this.httpserver = httpserver;
    }

    /**
     * 深圳市工务署，龙岗区工务署公用方法  上传基础信息
     */
    public JSONObject addBasicInfo(JSONObject jsonParam) {
        String url = httpserver + "/elevator/addBasicInfo";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String body = JSON.toJSONString(jsonParam);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(jsonParam, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);

    }


    /**
     * 深圳市工务署，龙岗区工务署公用方法  上传参数信息
     */

    public JSONObject addParams(JSONObject jsonParam) {
        String url = httpserver + "/elevator/addParams";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String body = JSON.toJSONString(jsonParam);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(jsonParam, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);
    }


    /**
     * 深圳市工务署，龙岗区工务署公用方法  上传实时数据
     */
    public JSONObject pushElevatorData(JSONObject jsonParam) {
        String url = httpserver + "/elevator/addRecord";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        List<Object> arrayList = new ArrayList<Object>();

        arrayList.add(jsonParam);

        String body = JSONArray.toJSONString(arrayList);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(arrayList, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);
    }


    /**
     * 龙岗区工务署上传操作数据
     */
    public JSONObject pushElevatorOperator(JSONObject jsonParam) {
        String url = httpserver + "elevator/operator";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        List<Object> arrayList = new ArrayList<Object>();

        arrayList.add(jsonParam);

        String body = JSONArray.toJSONString(arrayList);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(arrayList, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);
    }

}
